<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Config;
use think\Session;
use think\Cache;

/**
 * 密码加密方法
 * @param string $pw 要加密的原始密码
 * @param string $authCode 加密字符串
 * @return string
 */
function st_password($pw, $authCode = '')
{
    if (empty($authCode)) {
        $authCode = Config::get('database.authcode');
    }
    $result = "###" . md5(md5($authCode . $pw));
    return $result;
}

/**
* base64转码图片
* @param $base64
* @param string $path
* @return bool|string
*/
function get_base64_img($base64,$path){
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
	    if($result[2] == 'jpeg'){
	        $image_name = uniqid().'.jpg';
	        //纯粹是看jpeg不爽才替换的
	    }else{
	        $image_name = uniqid().'.'.$result[2];
	    }
	    $new_file = $path.time()."{$image_name}";
	    if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){
	    return time().$image_name;
	    }else{
	    return  false;
	    }
    }
}

function doCurl($url){
	$curl = curl_init();
	// 使用curl_setopt()设置要获取的URL地址
	curl_setopt($curl, CURLOPT_URL, $url);
	// 设置是否输出header
	curl_setopt($curl, CURLOPT_HEADER, false);
	// 设置是否输出结果
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	// 设置是否检查服务器端的证书
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	// 使用curl_exec()将CURL返回的结果转换成正常数据并保存到一个变量
	$data = curl_exec($curl);
	// 使用 curl_close() 关闭CURL会话
	curl_close($curl);
	return json_decode($data);
}
/**
	*	lhp
	*	2018年7月6日
	*	返回json数据
	*	@return $data 返回的数据
	*	@return $message 返回的信息
	*	@return $code 	返回的状态码 
	*/
	function returnAjax($data="",$message="",$code=""){
		$arr = array();
		//如果code为空
		if(empty($code)){
			$arr = [
				'code' => 0,
				'message' => $message,
				'data' => $data
			];
		}
		//如果message为空
		if(empty($message)){
			$arr = [
				'code' => $code,
				'message' => "",
				'data' => $data
			];
		}
		//如果code 和 message 都为空
		if(empty($message) && empty($code)){
			$arr = [
				'code' => 0,
				'message' => "",
				'data' => $data
			];
		}
		//如果data为空
		if(empty($data)){
			$arr = [
				'code' => $code,
				'message' => $message,
				'data' => ""
			];
		}
		//如果都为空
		if(empty($data) && empty($code) && empty($message)){
			$arr = [
				'code' => 0,
				'message' => "",
				'data' => ""
			];
		}
		if(empty($data) && empty($code) && empty($message)){
			$arr = [
				'code' => 0,
				'message' => "",
				'data' => $data
			];
		}
		return $arr;
	}


	/**
 * 产生随机字符串
 * @param  string $length   字符串长度
 * @param  int    $type     生成字符串类型,0(默认):26字母大小写+数字,1:数字，2:26字母小写，3:26字母大写，4:26字母大小写，5:字母小写+数字，6:字母大写+数字
 * @return string           返回产生随机字符串
 */
function random($length = 1, $type = 0, $patten ='') {
	$str = "";
    if (!$patten) {
        switch ($type) {
            case 1:
                $patten = '0123456789';
                break;
            case 2:
                $patten = 'abcdefghigklmnopqrstuvwxyz';
                break;
            case 3:
                $patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case 4:
                $patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz';
                break;
            case 5:
                $patten = 'abcdefghigklmnopqrstuvwxyz0123456789';
                break;
            case 6:
                $patten = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
                break;
            default:
                $patten = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghigkmnpqrstuvwxyz0123456789';
                break;
        }
    }
    for($i=1;$i>0;$i++){
        if(strlen($str)<$length){
            $rand=rand(0,strlen($patten));
            $str.= substr($patten, $rand, 1);
        }else{
            break;
        }
    }
    return $str;
}
/**
 * 生成唯一标识
 */
function GenerateToken($type="")
{
	return md5(uniqid(md5(microtime(true)),true).$type);
}

/**
	 * 日志记录
	 */
	function setLog($data,$type){
		$date = date('Y-m-d');
		$filename = './log/'.$type.'/'.$date.'.txt';
		$dir = iconv("UTF-8", "GBK", './log/'.$type);
        if (!file_exists($dir)){
            mkdir ($dir,0777,true);
        } 
		$file = fopen($filename, 'a+');
		$content;
		if(is_array($data)){
			$content = var_export($data,true);
		}else{
			$content = $data.date('Y-m-d H:i:s')."\r\n";
		}
		fwrite($file,$content);
	}

/**
 * 获取当前时间
 */ 
function getNowDate(){
	return date('Y-m-d H:i:s',time());
}
	/**
	 * 获取未来时间
	 */
function  future_time($now,$month){
	return  date("Y-m-d H:i:s",strtotime("+{$month}months",strtotime($now)));  
}

/**
 * 会员卡号生成规则
 */
function card_rule($area,$userNumber){
	$year = date('y');
	$month = date('m');
	$day = date('d');
	return $area.$month.$day.$year.$userNumber;
}

/**
 * 用户编号
 */
function setuserNumber($number){
	$var=sprintf("%04d",$number);
	return $var;
}

/**
 * 支付时间转化
 */
function payTimeTo($time){
	$year = substr($time,0,4);
	$month = substr($time,4,2);
	$day = substr($time,6,2);
	$hour = substr($time,8,2);
	$minutes = substr($time,10,2);
	$second = substr($time,12,2);
	return $year.'-'.$month.'-'.$day.' '.$hour.':'.$minutes.':'.$second;
}

